package com.jvm123.excel.in;

import com.jvm123.excel.common.DefaultExcelReader;
import com.jvm123.excel.common.ExcelProperties;
import com.jvm123.excel.common.ExcelReader;
import com.jvm123.excel.in.impl.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class ExcelImporter {

    private static final Logger logger = LoggerFactory.getLogger(ExcelImporter.class);

    private ExcelProperties excelProperties;
    private ExcelReader excelReader;
    private ExcelHelper excelHelper;

    private MapConverter mapConverter;
    private BeanConverter beanConverter;

    private FieldTypeBinder fieldTypeBinder;

    private List<String> commentList;
    private List<String> nameList;

    private void init() {
        if (excelProperties == null) {
            // 要读取Excel基本数据信息的配置
            excelProperties = new ExcelProperties();
        }
        if (excelReader == null) {
            // Excel读取对象（读取为Workbook）
            excelReader = new DefaultExcelReader();
        }
        if (excelHelper == null) {
            // Excel工具（读取头部行等）
            excelHelper = new DefaultExcelHelper(excelProperties);
        }
        if (fieldTypeBinder == null) {
            // 读取的数据与 bean 的属性类型之间的转换器
            fieldTypeBinder = new DefaultFieldTypeBinder();
        }
        if (mapConverter == null) {
            mapConverter = new MapConverterImpl();
        }
        if (beanConverter == null) {
            beanConverter = new BeanConverterImpl(fieldTypeBinder);
        }
        logger.debug("转换器（converter）初始化完成：reader:{}, fieldTypeBinder:{}", excelReader, fieldTypeBinder);
    }

    public List<Map<String, Object>> convertToMap(File file) {
        init();
        return convertToMap(excelReader.read(file));
    }

    public List<Map<String, Object>> convertToMap(InputStream inputStream) {
        init();
        return convertToMap(excelReader.read(inputStream));
    }

    public List<Map<String, Object>> convertToMap(String filePath) {
        init();
        return convertToMap(excelReader.read(filePath));
    }

    public <T> List<T> convert(File file, Class<T> tClass) {
        init();
        return convert(excelReader.read(file), tClass);
    }

    public <T> List<T> convert(InputStream inputStream, Class<T> tClass) {
        init();
        return convert(excelReader.read(inputStream), tClass);
    }

    public <T> List<T> convert(String filePath, Class<T> tClass) {
        init();
        return convert(excelReader.read(filePath), tClass);
    }

    private List<Map<String, Object>> convertToMap(Workbook workbook) {
        commentList = excelHelper.getCommentList(workbook);
        nameList = excelHelper.getNameList(workbook);
        // Map转换对象（将excel中的数据转化为mapList）
        mapConverter = new MapConverterImpl(excelProperties, nameList);
        return mapConverter.convertToMapList(workbook);
    }

    private <T> List<T> convert(Workbook workbook, Class<T> tClass) {
        commentList = excelHelper.getCommentList(workbook);
        nameList = excelHelper.getNameList(workbook);
        // Map转换对象（将excel中的数据转化为mapList）
        mapConverter = new MapConverterImpl(excelProperties, nameList);
        List<Map<String, Object>> mapList = mapConverter.convertToMapList(workbook);
        // bean转换对象（将mapList转换为beanList）
        beanConverter = new BeanConverterImpl(fieldTypeBinder);
        return beanConverter.convertToBeanList(tClass, mapList);
    }

    public ExcelProperties getExcelProperties() {
        return excelProperties;
    }

    public void setExcelProperties(ExcelProperties excelProperties) {
        this.excelProperties = excelProperties;
    }

    public ExcelReader getExcelReader() {
        return excelReader;
    }

    public void setExcelReader(ExcelReader excelReader) {
        this.excelReader = excelReader;
    }

    public ExcelHelper getExcelHelper() {
        return excelHelper;
    }

    public void setExcelHelper(ExcelHelper excelHelper) {
        this.excelHelper = excelHelper;
    }

    public MapConverter getMapConverter() {
        return mapConverter;
    }

    public void setMapConverter(MapConverter mapConverter) {
        this.mapConverter = mapConverter;
    }

    public BeanConverter getBeanConverter() {
        return beanConverter;
    }

    public void setBeanConverter(BeanConverter beanConverter) {
        this.beanConverter = beanConverter;
    }

    public FieldTypeBinder getFieldTypeBinder() {
        return fieldTypeBinder;
    }

    public void setFieldTypeBinder(FieldTypeBinder fieldTypeBinder) {
        this.fieldTypeBinder = fieldTypeBinder;
    }

    //
    public List<String> getCommentList() {
        return commentList;
    }

    public List<String> getNameList() {
        return nameList;
    }

}
